import request from '@/utils/request'
import type { Character } from '@/types/character'

interface ApiResponse<T> {
  data: {
    success: boolean
    error?: string
    message?: string
    characters?: T
  }
}

export const characterApi = {
  // 获取所有角色
  initCharacters: async (): Promise<Character[]> => {
    const response = await request.get<ApiResponse<Character[]>>('/character_config/initCharacters')
    if (!response.data.success) {
      throw new Error(response.data.error || '获取角色列表失败')
    }
    return response.data.characters || []
  },

  // 保存角色列表
  saveCharacter: async (characters: Character[]): Promise<void> => {
    const response = await request.post<ApiResponse<null>>('/character_config/saveCharacter', { characters })
    if (!response.data.success) {
      throw new Error(response.data.error || '保存角色失败')
    }
  },

  // 删除角色
  delCharacter: async (characterId: string): Promise<void> => {
    const response = await request.post<ApiResponse<null>>('/character_config/delCharacter', { character_id: characterId })
    if (!response.data.success) {
      throw new Error(response.data.error || '删除角色失败')
    }
  },
}

export default characterApi
